/** page */
function toPage(page) {
	if (page == null)
		return;
	var pageurl = $("#pageurl").val();
	var pageform = $("#pageform").val();
	var param = $("#" + pageform).serialize();
	param += '&pageNo=' + page;
	var url = pageurl;
	trans2(url, "", param);
}
function form_save(form,url,txt) {
	if (!$('#'+form).valid()) {
		return;
	}
	var param = $("#"+form).serialize(); // 获取表单数据
	console.log(param);
	trans2(url, txt, param);
}

function form_submit(form,url,txt) {
	var param = $("#"+form).serialize(); // 获取表单数据
	trans2(url, txt, param);
}


function list_del(url) {
	var title="<h2>确认删除?</h2>";
	bootbox.confirm(title, function(result){
		if(result) {
			trans2(url);
		}
	});
}

function list_reset(url) {
	var title="<h2>确认清除位置?</h2>";
	bootbox.confirm(title, function(result){
		if(result) {
			trans2(url);
		}
	});
}

/** user begin */
function user_updateState(id, state) {
	var title;
	if (state == 0) {
		title = "<h2>确认启用?</h2>";
		user_updateConfirm(id, title, state);
	} else {
		title = "<h3>确认禁用?</h3>";
		user_updateConfirm(id, title, state);
	}
}
function user_updateConfirm(id, title, state) {
	bootbox.confirm(title, function(result) {
		if (result) {
			trans2('/system/user_delete?id=' + id + "&state=" + state);
		}
	});
}
/** user end */

function sensor_zero(code,table,id,type) {
	$('#writeModal').modal({
		backdrop: 'static',
		show: true,
		keyboard: false
	});
	$.ajax({
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		type : 'post',
		url : root+'/sensor/zero/'+code+'/'+type+'/'+id+'/'+table+'?se='+ new Date().getTime(),
		success : function(data){
			$('#writeModal').modal('hide');
			if(data=='0'){
				_my_alert('设置成功','设置传感器'+code+'信息成功！');
			}else {
				_my_alert('设置失败',data);
			}
		},
		error : function(req) {
			$('#writeModal').modal('hide');
			_my_alert('设置失败','设置传感器'+code+'信息失败！，请检查串口');
		}
	});
}
function sensor_init(id) {
	var wait = root+"/resources/images/ajax-loader-5.gif";
	$('#wait').attr('src', wait);
	$('#writeModal').modal('show');

	var url = "/sensor/level_init";
	ajaxSubmit(url, {id:id}, 'post', function(data) { // data : 0 成功 其他错误
		$('#writeModal').modal('hide');
    	if (data == 0) {
			_my_alert("提示", "设置初始值成功!");
    	} else {
			_my_alert("提示", "设置初始值失败!");
    	}
    	trans2('/sensor/level',null,null,function(){$('.modal-backdrop.fade.in').remove();});
	},function(){_my_alert("提示", "串口错误!");$('.modal-backdrop.fade.in').remove();});
}

function readData(table, url2) {
	var wait = root+"/resources/images/ajax-loader-5.gif";
	$('#wait').attr('src', wait);
	$('#writeModal').modal('show');

	var url = "/read/data";
	ajaxSubmit(url, {table:table}, 'post', function(data) { // data : 0 成功 其他错误
		$('#writeModal').modal('hide');
    	if (data == 0) {
			_my_alert("提示", "读取数据成功!");
    	} else {
			_my_alert("提示", data);
    	}
    	trans2(url2,null,null,function(){$('.modal-backdrop.fade.in').remove();});
	},function(){_my_alert("提示", "串口错误!");$('.modal-backdrop.fade.in').remove();});
}

function confirmRainTimeBox(type) {
	var title;
	var url;
	if (type==1) {
		title = "<h2>确认设置雨量计时间?</h2>";
		url = "/read/setRainTime";
	} else {
		title = "<h2>确认读取雨量计时间?</h2>";
		url = "/read/getRainTime";
	}
	var code = $('#code').val();
	bootbox.confirm(title, function(result){
		if(result) {
			var wait = root+"/resources/images/ajax-loader-5.gif";
			$('#wait').attr('src', wait);
			$('#writeModal').modal('show');

			ajaxSubmit(url, {code:code}, 'post', function(data) { // data : 0 成功 1 串口错误
				$('#writeModal').modal('hide');
				if (data == 0) { // setRainTime success
					_my_alert("提示", "设置时间成功!");
				} else if (data == 1) { // set/getRainTime serialPort == null
					_my_alert("提示", "串口错误!");
				} else if (data == 2 || data == 3) { // set/getRainTime exception
					_my_alert("提示", "异常错误!");
				} else { // getRainTime time
					_my_alert("提示", "雨量计时间为："+data);
				}
			});
		}
	});
}
